﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Kalficom.Libraies
{
    class ctl
    {
        Libraies.msg msg = new Libraies.msg();
        internal Boolean check_ComboValue(ComboBox[] cbos)
        {
            
            foreach (ComboBox cbo in cbos)
            {
                //if (cbo.DroppedDown) cbo.DroppedDown = false;
                //cbo.SelectedIndex = cbo.FindStringExact(cbo.Text);
                if (cbo.FindStringExact(cbo.Text) == -1 && cbo.Visible)
                {
                    MessageBox.Show(string.Format(msg.ctlComboText, cbo.Text), msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    cbo.Focus();
                    return false;
                }
            }
            return true;
        }

        internal void ComboBox_Style(ComboBox[] cbos)
        {
            foreach (ComboBox cbo in cbos)
            {
                cbo.KeyDown += cbo_KeyDown;
                cbo.Leave += cbo_Leave;
            }
        }

        void cbo_Leave(object sender, EventArgs e)
        {
            ComboBox cbo = (ComboBox)sender;
            cbo.Text = cbo.Text;
        }

        void cbo_KeyDown(object sender, KeyEventArgs e)
        {
            ComboBox cbo = (ComboBox)sender;
            
            if (e.KeyCode == Keys.Enter)
            {
                if (cbo.FindStringExact(cbo.Text) == -1)
                { MessageBox.Show(string.Format(msg.ctlComboText, cbo.Text), msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
                else
                { cbo.SelectedIndex = cbo.FindStringExact(cbo.Text); }
            }
            else
            {
                if (!cbo.DroppedDown) cbo.DroppedDown = true;
            }
        }

        internal void DateTimeTextBox(TextBox txt, DateTimePicker dtp, Boolean allowNull = false)
        {
            if (allowNull && txt.Text.Trim() == "") return;
            try
            {
                string[] arr = txt.Text.Split(char.Parse("/"));
                DateTime dateValue;
                if (DateTime.TryParse(string.Format("{0}/{1}/{2}", arr[2], arr[1], arr[0]), out dateValue))
                {
                    // Conversion succedded
                    dtp.Value = dateValue;
                    txt.Text = dtp.Value.Date.ToString("dd/MM/yyyy");
                }
                else
                {
                    // Conversion failed
                    MessageBox.Show(msg.textValidatedDate, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txt.Focus();
                    txt.SelectAll();
                }
            }
            catch
            {
                MessageBox.Show(msg.textValidatedDate, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txt.Focus();
                txt.SelectAll();
            }

        }
    }
}
